package com.xuecheng.media.service.impl;

import com.xuecheng.base.constant.MediaObjectAuditConstant;
import com.xuecheng.media.exception.MediaFileNotExistException;
import com.xuecheng.media.model.dto.AuditFailDTO;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.IMediaAuditService;
import com.xuecheng.media.service.IMediaFilesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @description: 媒资审核服务
 * @author 勾新杰
 * @version 1.0
 * @date 2025/2/16 20:28
 */
@Service
@RequiredArgsConstructor
public class MediaAuditServiceImpl implements IMediaAuditService {

    /**
     * 媒资文件服务
     */
    private final IMediaFilesService mediaFilesService;

    /**
     * 审核成功
     *
     * @param mediaId 媒资文件id
     */
    @Override
    public void auditSuccess(String mediaId) {
        // 1. 查询媒资文件看是否存在
        MediaFiles mediaFiles = mediaFilesService.lambdaQuery()
                .eq(MediaFiles::getId, mediaId)
                .one();
        if (mediaFiles == null) MediaFileNotExistException.cast();
        // 2. 更新媒资文件审核状态为审核成功
        mediaFilesService.lambdaUpdate()
                .eq(MediaFiles::getId, mediaId)
                .set(MediaFiles::getAuditStatus, MediaObjectAuditConstant.AUDIT_SUCCESS)
                .set(MediaFiles::getChangeDate, LocalDateTime.now())
                .set(MediaFiles::getAuditMind, null)
                .update();
    }

    /**
     * 审核失败
     *
     * @param mediaId 媒资文件id
     */
    @Override
    public void auditFail(String mediaId, AuditFailDTO auditFailDTO) {
        // 1. 查询媒资文件看是否存在
        MediaFiles mediaFiles = mediaFilesService.lambdaQuery()
                .eq(MediaFiles::getId, mediaId)
                .one();
        if (mediaFiles == null) MediaFileNotExistException.cast();
        // 2. 更新媒资文件审核状态为审核失败
        mediaFilesService.lambdaUpdate()
                .eq(MediaFiles::getId, mediaId)
                .set(MediaFiles::getAuditStatus, MediaObjectAuditConstant.AUDIT_FAILED)
                .set(MediaFiles::getAuditMind, auditFailDTO.getAuditMind())
                .set(MediaFiles::getChangeDate, LocalDateTime.now())
                .update();
    }
}
